package edu.uwosh.cs342.group5.Route;

/*
 * Classes Route, Road, Point, KMLHandler, RouteOverlay borrows code from:
 * http://code.google.com/p/j2memaprouteprovider/source/browse/#svn%2Ftrunk%2FJ2MEMapRouteAndroidEx%2Fsrc%2Forg%2Fci%2Fgeo%2Froute%253Fstate%253Dclosed
 */

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import android.os.Handler;
import com.google.android.maps.GeoPoint;



import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class Route {
	
	public String getURL(double currLat, double currLon, double destLat,
			double destLon) {
		StringBuffer urlString = new StringBuffer();
		urlString.append("http://maps.google.com/maps?f=d&hl=en");
		urlString.append("&saddr=");// from
		urlString.append(Double.toString(currLat));
		urlString.append(",");
		urlString.append(Double.toString(currLon));
		urlString.append("&daddr=");// to
		urlString.append(Double.toString(destLat));
		urlString.append(",");
		urlString.append(Double.toString(destLon));
		urlString.append("&ie=UTF8&0&om=0&output=kml");
		return urlString.toString();
	}

	public InputStream getConnection(String url) {
		InputStream is = null;
		try {
			URLConnection conn = new URL(url).openConnection();
			is = conn.getInputStream();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return is;
	}
	
	public static Road getRoute(InputStream is) {
		KMLHandler handler = new KMLHandler();
		try {
			SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
			parser.parse(is, handler);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return handler.getRoad();
	}
}